float4x4 World;
float4x4 View;
float4x4 Projection;

float3 LightPosition, Ambient, Diffuse, Specular, CameraPosition;	//world space
float Sheen;

int Offset, Tiling;
texture Texture;

sampler NormalSampler = sampler_state
{
	Texture = <Texture>;
	AddressU = Wrap;
	AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
	float3 Normal : NORMAL0;
	float2 UV: TEXCOORD0;

    // TODO: add input channels such as texture
    // coordinates and vertex colors here.
};

struct PhongVertexShaderOutput
{
	float4 Position : POSITION0;
	float2 UV : TEXCOORD0;
	float4 WorldPosition : TEXCOORD1;
	float3 WorldNormal : TEXCOORD2;
};

PhongVertexShaderOutput PhongVertex(VertexShaderInput input)
{
	PhongVertexShaderOutput output;

	output.WorldPosition = mul(input.Position, World);
	float4 viewPosition = mul(output.WorldPosition, View);
	output.Position = mul(viewPosition, Projection);
	output.WorldNormal = mul(input.Normal, World);
	output.UV = (input.UV * Tiling) + Offset;

	return output;
}

float4 PhongPixel(PhongVertexShaderOutput input) : COLOR0
{
	float3 normal = tex2D(NormalSampler, input.UV);
	normal = mul(normal, World);

	float3 lightDirection = normalize(LightPosition - input.WorldPosition.xyz);
	float3 viewDirection = normalize(CameraPosition - input.WorldPosition.xyz);
	float3 reflection = -reflect(lightDirection, normal);
	input.WorldNormal = normalize(normal);

	
										
	float3 diffuse = max(dot(lightDirection, normal), 0) * Diffuse;
	float3 specular = max(pow(dot(reflection, viewDirection), Sheen), 0) * Specular;
	float3 color =  diffuse + specular + Ambient;
	return float4(color, 1);
}

technique Phong
{
	pass Pass1
	{
		// TODO: set renderstates here.

		VertexShader = compile vs_2_0 PhongVertex();
		PixelShader = compile ps_2_0 PhongPixel();
	}
}